$(document).ready(function() {
	var sorce = decodeURIComponent((new RegExp('[?|&]sorce=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	var productid = decodeURIComponent((new RegExp('[?|&]product_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	var lucky_record_id = decodeURIComponent((new RegExp('[?|&]lucky_record_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	var datac = decodeURIComponent((new RegExp('[?|&]datac=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	
	
	var is_default = $('.checkbox').attr('data-default')
	if (is_default ==1) {
	
		$('.defaultcheckbox ').addClass('defaultcheckbox_cur')
		$('.checkbox').is(':checked')	
	}

	$('.defaultcheckbox').click(function(event) {
		if ($('.checkbox').is(':checked')) {
			$('.defaultcheckbox').addClass('defaultcheckbox_cur')
		} else {
			$('.defaultcheckbox').removeClass('defaultcheckbox_cur')
		}
	});
	
	
	if(getCookie('Provinces')){
		var url_e = JSON.parse(getCookie('Provinces'))
	}else{
		var arr = {}
		arr.p = 1
		arr.c = 1
		arr.pid = 1
		var Provinces = JSON.stringify(arr);
		SetCookie('Provinces',Provinces )
		var url_e = JSON.parse(getCookie('Provinces'))
	}
	
	// 默认为省
	$.ajax({
			url: url,
			type: 'get',
			success: function(data) {
				var data = JSON.parse(data).data;
				var html = '';
				
				
				for (var i = 0; i < data.length; i++) {
					html += '<option data-pid="' + data[i].province_id + '" value ="' + data[i].name + '"  data-ids="'+data[i].province_id+'">' + data[i].name + '</option>'
				}
				$('.province').html(html)

				if (url_e !== null && url_e.p != '') {
					$(".province").find("option[value=" + url_e.p +"]").attr('selected', true);
					// var c_id = $(".province").find("option[value=" + url_e.p +"]").attr('data-ids');
					// var arr ={}
					// arr.c_id = c_id
					// var citys = JSON.stringify(arr);
					// SetCookie('citys',citys )

				}


			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})


	
	var pid = 1;
	if (url_e !== null && url_e.pid != '') {
		pid = url_e.pid
	}

//市
	$.ajax({
		url: url,
		type: 'get',
		data: {
			province_id: pid
		},
		success: function(data) {
			var data = JSON.parse(data).data;
			var html = '';
			for (var i = 0; i < data.length; i++) {
				html += '<option data-cid="' + data[i].province_id + '"  value ="' + data[i].name + '">' + data[i].name + '</option>'
			}
			$('.citys').html(html)

			
			if (url_e !== null && url_e.c != '') {
				$(".citys").find("option[value=" +url_e.c +"]").attr('selected', true);
			}
		


		},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
	})
	// 城市联动
	$('.province').change(function(e) {
		
		var province_id = $('.province').find("option:selected").attr('data-pid')
			
		$.ajax({
			url: url,
			type: 'get',
			data: {
				province_id: province_id
			},
			success: function(data) {
				var data = JSON.parse(data).data;
				var html = '';
				for (var i = 0; i < data.length; i++) {
					html += '<option data-cid="' + data[i].province_id + '" value ="' + data[i].name + '">' + data[i].name + '</option>'
				}
				$('.citys').html(html)
			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})
		SetCookie('Provinces','')
	});
	// form表单验证
	// $(".addaddress_form").Validform({
	// 	btnSubmit: ".address_save",
	// 	ignoreHidden: true,
	// 	tiptype: 3,
	// 	// ajaxPost:true

	// })
		var address_id = decodeURIComponent((new RegExp('[?|&]address_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
	
	// 保存提交
	$('.address_save').click(function(event) {
		
		var name = $('#name').val()
		var tel = $('#tel').val()
		var province = $('.province').val()
		var city =  $('.citys').val()
		var province_id= $('.province').find("option:selected").attr('data-pid')
		var city_id = $('.citys').find("option:selected").attr('data-cid')
		var address = $('#address_textarea').val()
		var is_default = function() {
			if ($('.checkbox').is(':checked')) {
				return 1;
			} else {
				return 0
			}
		}
		$.ajax({
			url: edit,
			type: 'POST',
			data: {
				name:name,
				tel:tel,
				province:province,
				city:city,
				address:address,
				address_id: address_id,
				is_default:is_default,
				province_id:province_id,
				city_id:city_id
			},
			success:function(data){
				var data = JSON.parse(data)
				if(data.code == 1){
					var product_id = decodeURIComponent((new RegExp('[?|&]product_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
				 	var attr_id = decodeURIComponent((new RegExp('[?|&]attr_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
				 	var num = decodeURIComponent((new RegExp('[?|&]num=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
				 	
					if (!product_id) {
						window.location.href = href
					}else{
						if(sorce == 4){
							window.location.href = href +'?product_id='+product_id +'&lucky_record_id='+lucky_record_id+'&sorce='+sorce+'&datac='+datac
						}else{
							window.location.href = href +'?product_id='+product_id +'&attr_id='+attr_id+'&num='+num
						}
						
					}
					SetCookie('Provinces','')
				}else {
					alert(data.desc);
				}
				
			 	
			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})
		
	});
	// 删除
	$('.address_del').click(function(event) {
	
		$.ajax({
			url: del,
			type: 'post',
			data: {
				address_id: address_id
			},
			success: function(data) {
				console.log(data)
				window.location.href= href
				SetCookie('Provinces','')
			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})
	});

	

});